package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dto.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserFollowService;
import com.heima.utils.common.UserThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * APP用户关注信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements ApUserFollowService {

    private static final String FOLLOW_FAN_KEY = "follow_fan";

    @Autowired
    private ApUserMapper userMapper;

    @Autowired
    private ApUserFanMapper userFanMapper;

    @Autowired
    private CacheService cacheService;

    /**
     * 关注或取消关注
     * @param userRelationDto
     * @return
     */
    @Override
    public ResponseResult followOrUnfollow(UserRelationDto userRelationDto) {
        //1.判断是否登录
        ApUser user = UserThreadLocalUtil.getUser();
        if (user == null || user.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //2.参数校验
        if (userRelationDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //准备redis所需数据
        String key = FOLLOW_FAN_KEY + "_" + userRelationDto.getAuthorId();
        Integer authorId = userRelationDto.getAuthorId();
        Long articleId = userRelationDto.getArticleId();
        Short operation = userRelationDto.getOperation();

        if (userRelationDto.getOperation() == 0) {
            //3.保存用户关注表
            ApUserFollow apUserFollow = new ApUserFollow();
            apUserFollow.setUserId(user.getId());
            apUserFollow.setFollowId(userRelationDto.getAuthorId());
            ApUser authorUser = userMapper.selectById(userRelationDto.getAuthorId());
            if (authorUser != null) {
                apUserFollow.setFollowName(authorUser.getName());
            }
            apUserFollow.setLevel((short)1);
            apUserFollow.setIsNotice((short)1);
            apUserFollow.setCreatedTime(new Date());
            save(apUserFollow);

            //4.保存移动端用户粉丝表
            ApUserFan apUserFan = new ApUserFan();
            apUserFan.setUserId(userRelationDto.getAuthorId());
            user = userMapper.selectById(user.getId());
            apUserFan.setFansId(user.getId());
            apUserFan.setFansName(user.getName());
            apUserFan.setLevel((short)0);
            apUserFan.setCreatedTime(new Date());
            apUserFan.setIsDisplay((short)1);
            apUserFan.setIsShieldComment((short)0);
            apUserFan.setIsShieldLetter((short)0);
            userFanMapper.insert(apUserFan);

            //保存到redis
            cacheService.hPut(key,"authorId",String.valueOf(authorId));
            cacheService.hPut(key,"articleId",String.valueOf(articleId));
            cacheService.hPut(key,"operation",String.valueOf(operation));
        }else {
            remove(Wrappers.<ApUserFollow>lambdaQuery()
                    .eq(ApUserFollow::getUserId,user.getId())
                    .eq(ApUserFollow::getFollowId,userRelationDto.getAuthorId()));
            userFanMapper.delete(Wrappers.<ApUserFan>lambdaQuery()
                    .eq(ApUserFan::getUserId,userRelationDto.getAuthorId())
                    .eq(ApUserFan::getFansId,user.getId()));

            //删除redis中的数据
            cacheService.hDelete (key,"authorId","articleId","operation");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
